<?php
class AuctionCore{  

    public $customerId;
    public $auctionId;
    
    protected $_result;
    
    public function result(){
        
    }
    
    
    
    public function bid(){
        $timeout = time() + 1;            
        do{
            $retry = time() < $timeout;
            $auctionLock = Yii::app()->cache->get('bid_auction_lock_'.$this->auctionId);
            if($auctionLock !== false){
                var_dump('lock');
                usleep(1000);
                continue;
            }  
            Yii::app()->cache->set('bid_auction_lock_'.$this->auctionId,'bid_auction_lock_'.$this->auctionId.'_'.time());
            var_dump($this->customerId);
            $retry = false;
            
            
        }while($retry);  
        //sleep(1);
        Yii::app()->cache->delete('bid_auction_lock_'.$this->auctionId);
        return '1';
    }
    
    public function process2(){
        $lock = Yii::app()->cache->get('bid_auction_lock_'.$this->auctionId);
        if ($lock !== false){
            $timeOut = time() + 1;
            while (time() < $timeOut){
                
                $lock = Yii::app()->cache->get('bid_auction_lock_'.$this->auctionId);
               
                
                
                
            }
        }
        Yii::app()->cache->delete('bid_auction_lock_'.$this->auctionId);        
    }
    
    
    public function baseBid(){
        
    }
    
    
    
    
    public function bidq($auctionId,$customerId){
        $error = null;
        $connection = Yii::app()->db;
        $sql = 'SELECT * FROM auction_pool';
        $rows=$connection->createCommand ($sql)->queryAll();
        //var_dump($rows);
        //$query = 'INSERT auction_pool(auction_id) VALUES(1002)';
        //$connection->createCommand ($query)->query();
        //step 1 valid auction 
        $auctionValidSQL = '
        SELECT 
             *
        FROM
            auction_pool
        WHERE
            auction_pool.auction_id = '.$auctionId.'
        ';
        
        $aucitonInfo = $connection->createCommand ($auctionValidSQL)->queryAll();
        
        if (empty($aucitonInfo)){
            $error = 'auction no';
            return $error;
        }
        
        $customerValidSQL = '
        SELECT
            *
        FROM
            customer
        WHERE
            customer.id = '.$customerId.'
        ';
        $customerInfo = $connection->createCommand ($customerValidSQL)->queryAll();
        if (empty($customerInfo)){
            $error = 'customer no';
            return $error;
        }
        $lock = Yii::app()->cache->get('bid_auction_lock_'.$auctionId);
        if ($lock !== false){
            $error = 'lock';
            $rTime = time()+1;
            $number = 0;
            while (time() < $rTime){
              //  echo '<br>'.time().'<br>';
                $number ++;
                // 1/1000 s
                usleep(1000);
            }
            var_dump($number);
            return $error;
        }
       
        Yii::app()->cache->set('bid_auction_lock_'.$auctionId,'bid_auction_lock_'.$auctionId.'_'.time());
        $lock2 = Yii::app()->cache->get('bid_auction_lock_'.$auctionId);
        var_dump($lock2);
        
        
        
        
        
        
        //Yii::app()->cache->delete('bid_auction_lock_'.$auctionId);
        
        //Yii::app()->cache->set($auctionId,'lock');
        
        //step 2 valid customer
        
        //step 3 
        
        
    }
    
    public function createCustomer($number){
        $connection = Yii::app()->db;
        $maxCustomerSql = '
        SELECT
            IFNULL(MAX(id),0) as maxCustomerId
        FROM
            customer
        ';
        $row = $connection->createCommand ($maxCustomerSql)->queryAll();
        $maxId = $row[0]['maxCustomerId'];
        
        
    }
    
}
